//
//  LoginMVM.swift
//  DianXia
//
//  Created by you&me on 2017/12/15.
//  Copyright © 2017年 you&me. All rights reserved.
//

import Foundation

class LoginVM {

     let provider = MoyaProvider<BCModuleApi>()
     let dispose = DisposeBag()

    func regist(_ name: String, _ mobile: String, _ pMobile: String, _ code: String, _ success:@escaping ()->()) {
        provider.rx.request(.regist(name: name, mobile: mobile, pMobile: pMobile, code: code))
                .asObservable()
                .retry(3)
                .mapModel(LoginModel.self)
                .subscribe(onNext: { (loginModel) in
                    guard loginModel.msg == "success" else {
                        return BCShowMessage.shareMessage.showTipMsg(loginModel.result?.describe ?? "", time: 1)
                    }
                    BCShowMessage.shareMessage.showTipMsg("注册成功、请等待短信通知再进行登录", time: 1)
                    success()
                    })
                .disposed(by: dispose)

    }
    /// 获取验证码
    ///
    /// - Parameter phoneText: 电话号码
    func getLoginPhoneCode(_ phoneText: String, _ loginOrRegist: String) {
        provider.rx.request(.getMobileCode(mobile: phoneText, destination: loginOrRegist))
                .asObservable()
                .retry(3)
                .mapModel(LoginModel.self)
                .subscribe(onNext: { (loginModel) in
                    if loginModel.msg == "fail" {
                        BCShowMessage.shareMessage.showTipMsg(loginModel.result?.describe ?? "", time: 1)
                    }
                })
                .disposed(by: dispose)
    }

   /// 登录
   ///
   /// - Parameters:
   ///   - phoneText: 电话号码
   ///   - code: 验证码
     func bcLogin(_ phoneText: String, _ code: String , _  success: @escaping ()->()) {

        provider.rx.request(.goLogin(mobile: phoneText, code: code))
                .asObservable()
                .retry(3)
                .mapModel(LoginModel.self)
                .subscribe(onNext: { (loginModel) in
                    guard loginModel.msg == "success" else {
                     return BCShowMessage.shareMessage.showTipMsg(loginModel.result?.describe ?? "", time: 1)
                    }
                    
                    UserManager.shareManager.isLogin = true
                    UserManager.shareManager.userId = loginModel.result?.userId
                    UserManager.shareManager.userName = loginModel.result?.userName
                    UserManager.shareManager.userMobile = loginModel.result?.userMobile
                    UserManager.shareManager.leaderId = loginModel.result?.leaderId
                    UserManager.shareManager.leaderName = loginModel.result?.leaderName
                    UserManager.shareManager.leaderMobile = loginModel.result?.leaderMobile
                    UserManager.shareManager.business = loginModel.result?.business
                    
                    success()
                  })
                .disposed(by: dispose)
    }
}

struct LoginModel: HandyJSON {
    var msg: String?
    var result: ResultModel?
}
struct ResultModel: HandyJSON {
    var describe: String?
    var userId: String?
    var userName: String?
    var userMobile: String?
    var leaderId: String?
    var leaderName: String?
    var leaderMobile: String?
    var business: String?

}
